package com.ellison.demo.example;

import com.ellison.demo.common.entity.WorkDoc;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>TODO</p>
 *
 * @author Ellison_Pei
 * @date 2021/10/25 14:10
 * @since 1.0
 **/
@Slf4j
public class StreamTest {
    public static void main(String[] args) {
        // 计算空字符串
        List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
        long count = 0L;
        System.out.println("使用 Java 8: ");
        System.out.println("列表: " + strings);

        count = strings.stream().filter(string -> string.isEmpty()).count();
        System.out.println("空字符串数量为: " + count);

        count = strings.stream().filter(string -> string.length() == 3).count();
        System.out.println("字符串长度为 3 的数量为: " + count);

        List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
        System.out.println("筛选后的列表: " + filtered);

        String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));
        System.out.println("合并字符串: " + mergedString);

        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        List<Integer> integers = Arrays.asList(1, 2, 13, 4, 15, 6, 17, 8, 19);
        List<Integer> squaresList = numbers.stream().map(i -> i * i).distinct().collect(Collectors.toList());
        System.out.println("Squares List: " + squaresList);
        System.out.println("列表: " + integers);

        IntSummaryStatistics stats = integers.stream().mapToInt((x) -> x).summaryStatistics();

        System.out.println("列表中最大的数 : " + stats.getMax());
        System.out.println("列表中最小的数 : " + stats.getMin());
        System.out.println("所有数之和 : " + stats.getSum());
        System.out.println("平均数 : " + stats.getAverage());
        System.out.println("随机数: ");

        // 输出10个随机数
        Random random = new Random();
        random.ints().limit(10).sorted().forEach(System.out::println);

        // 并行处理
//        count = strings.parallelStream().filter(string -> string.isEmpty()).count();
        count = strings.parallelStream().filter(String::isEmpty).count();
        System.out.println("空字符串的数量为: " + count);
    }


    public static WorkDoc getNewInstance(String param) {
        WorkDoc workDoc = new WorkDoc();
        workDoc.setId(0L);
        workDoc.setFactory(param);
        workDoc.setWorkDocCode(param);
        workDoc.setRelatedBillId(0L);
        workDoc.setRelatedBillCode(param);
        workDoc.setLogisticsOrderNo(param);
        workDoc.setCarrier(param);
        workDoc.setJdBillCode(param);
        workDoc.setServiceNo(param);
        workDoc.setServiceProvider(param);
        workDoc.setMaterialDocumentCode(param);
        workDoc.setAllocatedQuantity(new BigDecimal("0"));
        workDoc.setMovedQuantity(new BigDecimal("0"));
        workDoc.setCustomerId(0L);
        workDoc.setCustomerCode(param);
        workDoc.setCustomerName(param);
        workDoc.setCity(param);
        workDoc.setShopAddress(param);
        workDoc.setShopName(param);
        workDoc.setShopContact(param);
        workDoc.setShopMobile(param);
        workDoc.setSuppId(0L);
        workDoc.setSuppCode(param);
        workDoc.setSuppName(param);
        workDoc.setStoragePlace(param);
        workDoc.setBillTypeId(0L);
        workDoc.setBillTypeCode(param);
        workDoc.setBillTypeName(param);
        workDoc.setTitle(param);
        workDoc.setRemark(param);
        workDoc.setWorkerName(param);
        workDoc.setCrtDepartment(param);
        workDoc.setCrtDepartmentName(param);
        workDoc.setStatus(param);
        workDoc.setIsPost(param);
        workDoc.setShopOrderCode(param);
        workDoc.setAttr1(param);
        workDoc.setAttr2(param);
        workDoc.setAttr3(param);
        workDoc.setAttr4(param);
        workDoc.setAttr5(param);
        workDoc.setAttr6(param);
        workDoc.setAttr7(param);
        workDoc.setAttr8(param);
        workDoc.setIsActive(param);
        workDoc.setVersion(0);
        workDoc.setTenantId(param);
        workDoc.setCrtUserName(param);
        workDoc.setCrtUserId(param);
        workDoc.setUpdUserName(param);
        workDoc.setUpdUserId(param);
        workDoc.setReceiptType(param);
        workDoc.setSoCode(param);
        workDoc.setInsuredValue(new BigDecimal("0"));

        return workDoc;

    }
}

@Slf4j
class PeekTest {
    public static void main(String[] args) {
        ArrayList<WorkDoc> javaList = Lists.newArrayList();
        // peek
        ArrayList<WorkDoc> list = Lists.newArrayList();
        WorkDoc workDoc = StreamTest.getNewInstance("java");
        list.add(workDoc);
        list.add(workDoc);
        WorkDoc workDoc1 = StreamTest.getNewInstance("c");
        list.add(workDoc1);
        WorkDoc workDoc2 = StreamTest.getNewInstance("javaScript");
        list.add(workDoc2);
        List<WorkDoc> collect = list.stream()
                .peek(w -> {
                    if ("java".equals(w.getAttr3())){
                        javaList.add(w);
                    }
                })
                .filter(workDoc3 -> workDoc3.getAttr1().equals("c"))
                .distinct()
                .collect(Collectors.toList());
        log.debug("修改后的list {}", collect);
    }
}
